package pers.liu.yibandemo.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import pers.liu.yibandemo.entity.Admin;
import pers.liu.yibandemo.service.AdminService;
import pers.liu.yibandemo.utils.JwtUtils;
import pers.liu.yibandemo.utils.Result;

@CrossOrigin
@RequestMapping("user")
@RestController
public class AdminController {
    @Autowired
    private AdminService adminService;
    @PostMapping(value = "/login")
    public Result login(@RequestBody Admin admin) {
        boolean isAllowLogin = adminService.getAdmin(admin);
        if(isAllowLogin){
            String token = JwtUtils.generateToken(admin.getAdminName());
            return Result.ok().data("token",token);
        }
        Result error = Result.error();
        error.setMessage("用户名或密码不对，请重新输入");
        return error;
    }
    @GetMapping(value = "info")
    public Result info(String token) {
        String username = JwtUtils.getClaimsByToken(token).getSubject();
        String avatar="https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif";
        Admin admin = adminService.getAdminByName(username);
        return Result.ok().data("name",username).data("avatar",avatar).data("id",admin.getAdminId());
    }
    @PostMapping("/logout")
    public Result logout(){
        return Result.ok();
    }
}
